<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="inventoryMovementNo">
        <el-input
          v-model="queryParams.inventoryMovementNo"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原货架" prop="sourceRackId">
        <el-select v-model="queryParams.sourceRackId" placeholder="请选择原货架" clearable>
          <el-option
            v-for="rack in rackPullData"
            :key="rack.id"
            :label="rack.rackName"
            :value="rack.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="目标货架" prop="targetRackId">
        <el-select v-model="queryParams.targetRackId" placeholder="请选择目标货架" clearable>
          <el-option
            v-for="rack in rackPullData"
            :key="rack.id"
            :label="rack.rackName"
            :value="rack.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in rackPullData"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="审核状态" prop="checkStatus">-->
<!--        <el-select v-model="queryParams.checkStatus" placeholder="请选择审核状态" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.wms_check_status"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="审核人" prop="checkUserId">-->
<!--        <el-input-->
<!--          v-model="queryParams.checkUserId"-->
<!--          placeholder="请输入审核人"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="审核时间">-->
<!--        <el-date-picker-->
<!--          v-model="daterangeCheckTime"-->
<!--          style="width: 240px"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          type="daterange"-->
<!--          range-separator="-"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--        ></el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:inventoryMovement:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:inventoryMovement:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:inventoryMovement:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:inventoryMovement:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inventoryMovementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一值" align="center" prop="id" />
      <el-table-column label="编号" align="center" prop="inventoryMovementNo" />
      <el-table-column label="原货架" align="center" prop="sourceRackId">

        <template slot-scope="scope">
          <rack-tag :options="rackPullData" :value="scope.row.sourceRackId"/>
        </template>


      </el-table-column>
      <el-table-column label="目标货架" align="center" prop="targetRackId">

        <template slot-scope="scope">
          <rack-tag :options="rackPullData" :value="scope.row.targetRackId"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_movement_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:inventoryMovement:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:inventoryMovement:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存移动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="inventoryMovementNo">
          <el-input v-model="form.inventoryMovementNo" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="原货架" prop="sourceRackId">
          <el-select v-model="form.sourceRackId" placeholder="请选择原货架">
            <el-option
              v-for="rack in rackPullData"
              :key="rack.id"
              :label="rack.rackName"
              :value="rack.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目标货架" prop="targetRackId">
          <el-select v-model="form.targetRackId" placeholder="请选择目标货架">
            <el-option
              v-for="rack in rackPullData"
              :key="rack.id"
              :label="rack.rackName"
              :value="rack.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.wms_movement_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">库存移动详情信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddWmsInventoryMovementDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteWmsInventoryMovementDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="wmsInventoryMovementDetailList" :row-class-name="rowWmsInventoryMovementDetailIndex" @selection-change="handleWmsInventoryMovementDetailSelectionChange" ref="wmsInventoryMovementDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="物料" prop="itemId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.itemId" placeholder="请输入物料" />
            </template>
          </el-table-column>
          <el-table-column label="计划数量" prop="planQuantity" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.planQuantity" placeholder="请输入计划数量" />
            </template>
          </el-table-column>
          <el-table-column label="实际数量" prop="realQuantity" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.realQuantity" placeholder="请输入实际数量" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
          <el-table-column label="源货架" prop="sourceRackId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sourceRackId" placeholder="请输入源货架" />
            </template>
          </el-table-column>
          <el-table-column label="源仓库" prop="sourceWarehouseId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sourceWarehouseId" placeholder="请输入源仓库" />
            </template>
          </el-table-column>
          <el-table-column label="源库区" prop="sourceAreaId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sourceAreaId" placeholder="请输入源库区" />
            </template>
          </el-table-column>
          <el-table-column label="移库状态" prop="moveStatus" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.moveStatus" placeholder="请选择移库状态">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="目标货架" prop="targetRackId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.targetRackId" placeholder="请输入目标货架" />
            </template>
          </el-table-column>
          <el-table-column label="目标仓库" prop="targetWarehouseId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.targetWarehouseId" placeholder="请输入目标仓库" />
            </template>
          </el-table-column>
          <el-table-column label="目标库区" prop="targetAreaId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.targetAreaId" placeholder="请输入目标库区" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInventoryMovement, getInventoryMovement, delInventoryMovement, addInventoryMovement, updateInventoryMovement } from "@/api/wms/inventoryMovement";
import { pullWarehouse } from '@/api/wms/warehouse'
import { pullArea } from '@/api/wms/area'
import { pullRack } from '@/api/wms/rack'

export default {
  name: "InventoryMovement",
  dicts: ['wms_check_status', 'wms_movement_status', 'sys_user_sex'],
  data() {
    return {
      warehousePullData: [],
      areaPullData: [],
      rackPullData: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWmsInventoryMovementDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存移动表格数据
      inventoryMovementList: [],
      // 库存移动详情表格数据
      wmsInventoryMovementDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 目标库区时间范围
      daterangeCheckTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inventoryMovementNo: null,
        sourceRackId: null,
        targetRackId: null,
        status: null,
        checkStatus: null,
        checkUserId: null,
        checkTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        inventoryMovementNo: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
        sourceRackId: [
          { required: true, message: "原货架不能为空", trigger: "change" }
        ],
        targetRackId: [
          { required: true, message: "目标货架不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    /** 查询仓库下拉数据 **/
    pullWarehouse().then(response => {
      this.warehousePullData = response.data;
    });

    /** 查询库区下拉数据 **/
    pullArea().then(response => {
      this.areaPullData = response.data;
    });

    /** 查询库区下拉数据 **/
    pullRack().then(response => {
      this.rackPullData = response.data;
    });
  },
  methods: {
    /** 查询库存移动列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCheckTime && '' != this.daterangeCheckTime) {
        this.queryParams.params["beginCheckTime"] = this.daterangeCheckTime[0];
        this.queryParams.params["endCheckTime"] = this.daterangeCheckTime[1];
      }
      listInventoryMovement(this.queryParams).then(response => {
        this.inventoryMovementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        inventoryMovementNo: null,
        sourceRackId: null,
        targetRackId: null,
        status: null,
        checkStatus: null,
        checkUserId: null,
        checkTime: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.wmsInventoryMovementDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCheckTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存移动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInventoryMovement(id).then(response => {
        this.form = response.data;
        this.wmsInventoryMovementDetailList = response.data.wmsInventoryMovementDetailList;
        this.open = true;
        this.title = "修改库存移动";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.wmsInventoryMovementDetailList = this.wmsInventoryMovementDetailList;
          if (this.form.id != null) {
            updateInventoryMovement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInventoryMovement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除库存移动编号为"' + ids + '"的数据项？').then(function() {
        return delInventoryMovement(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 库存移动详情序号 */
    rowWmsInventoryMovementDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 库存移动详情添加按钮操作 */
    handleAddWmsInventoryMovementDetail() {
      let obj = {};
      obj.itemId = "";
      obj.planQuantity = "";
      obj.realQuantity = "";
      obj.remark = "";
      obj.sourceRackId = "";
      obj.sourceWarehouseId = "";
      obj.sourceAreaId = "";
      obj.moveStatus = "";
      obj.targetRackId = "";
      obj.targetWarehouseId = "";
      obj.targetAreaId = "";
      this.wmsInventoryMovementDetailList.push(obj);
    },
    /** 库存移动详情删除按钮操作 */
    handleDeleteWmsInventoryMovementDetail() {
      if (this.checkedWmsInventoryMovementDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的库存移动详情数据");
      } else {
        const wmsInventoryMovementDetailList = this.wmsInventoryMovementDetailList;
        const checkedWmsInventoryMovementDetail = this.checkedWmsInventoryMovementDetail;
        this.wmsInventoryMovementDetailList = wmsInventoryMovementDetailList.filter(function(item) {
          return checkedWmsInventoryMovementDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleWmsInventoryMovementDetailSelectionChange(selection) {
      this.checkedWmsInventoryMovementDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/inventoryMovement/export', {
        ...this.queryParams
      }, `inventoryMovement_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
